package net.oschina.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.oschina.app.R;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.bean.TweetBean;
import net.oschina.app.util.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by wei03 on 2017/1/14.
 * 最新动弹的适配器
 */
public class TweetNewAdapter extends BaseAdapter {
    private Context mContext;

    public TweetNewAdapter(Context context) {
        super();
        mContext = context;
    }

    @Override
    public TweetNewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View itemView = CommonUtils.inflate(R.layout.item_tweet_new);
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_tweet_new, parent, false);
//        View itemView = CommonUtils.getInflate().inflate(R.layout.item_tweet_new, parent, false);
        TweetNewHolder tweetHotHolder = new TweetNewHolder(itemView);
        return tweetHotHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TweetNewHolder tweetNewHolder = (TweetNewHolder) holder;
        TweetBean tweetBean = (TweetBean) mDatas.get(position);
//       头像加载
        Glide.with(mContext).load(tweetBean.getPortrait()).into(tweetNewHolder.mIvIcont);
        tweetNewHolder.mTvUserName.setText(tweetBean.getAuthor());
        //富文本展示  上下文必须可以开任务栈
        CommonUtils.displayRichText(mContext, tweetBean.getBody(), tweetNewHolder.mTvDes);
        tweetNewHolder.mTvTime.setText(tweetBean.getPubDate());
        if (tweetBean.getLikeCount() != 0) {
            tweetNewHolder.mTvPraisecount.setText(String.valueOf(tweetBean.getLikeCount()));
        }
        if (tweetBean.getCommentCount() != 0) {
            tweetNewHolder.mTvCommentcount.setText(String.valueOf(tweetBean.getCommentCount()));
        }

        if(tweetBean.getImgSmall()!=null){
            tweetNewHolder.mIvPhoto.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(tweetBean.getImgSmall()).into(tweetNewHolder.mIvPhoto);
        }else {
            tweetNewHolder.mIvPhoto.setVisibility(View.GONE);
        }
    }

    //热门动弹的Holder
    static class TweetNewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_icont)
        CircleImageView mIvIcont;
        @BindView(R.id.tv_userName)
        TextView        mTvUserName;
        @BindView(R.id.tv_des)
        TextView        mTvDes;
        @BindView(R.id.tv_time)
        TextView        mTvTime;
        @BindView(R.id.tv_praisecount)
        TextView        mTvPraisecount;
        @BindView(R.id.tv_commentcount)
        TextView        mTvCommentcount;
        @BindView(R.id.iv_photo)
        PhotoView       mIvPhoto;
//        @BindView(R.id.iv_like)
//        ImageView       mIvLike;

        TweetNewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}
